// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env:"cloud1-9gi73u0tdf91dfdb"})

// 云函数入口函数
exports.main = async (event, context) => {
  cloud.database().collection('user').add({
    data:{
       _id:event.nDate,
       title:event.title,
       zone:event.zone,
       content:event.content,
       reward:event.reward,
       comment:[],
       fileID:event.fileID,
       username:event.username,
       userid:event.userid,
       myDate:event.PDate,
       img:event.img
    },
    sucess(res){
      return res
    },
    fail(err){
      return err
    }
  })
   

  var mypublish={
    '_id':event.nDate,
    'title':event.title,
    'content':event.content,
    'zone':event.zone,
    'date':event.PDate
  }
  
  
  cloud.database().collection('user_Info').where({
    openid:event.userid
  }).get().then(res=>{
      var myPublish=res.data[0]['myPublish']
      myPublish.push(mypublish)
    cloud.database().collection('user_Info').doc(res.data[0]['_id']).update({
        data:{
          myPublish: myPublish
        },
        sucess(res){
          return res
        },
        fail(err){
          return err
        }
      })
  }).catch(err=>{
  console.log(err)//打印错误信息
  })

}